package com.gem.entity;


import com.gem.card.AdvancedCard;
import com.gem.card.MediumCard;
import com.gem.card.PrimaryCard;
import com.gem.enums.CardType;
import com.gem.netty.entity.CardInfo;
import lombok.Data;

import java.util.*;

/**
 * Author: Xhy
 * CreateTime: 2022-09-17 15:18
 * card info
 */
@Data
public class Card {

    private static Map<Integer,Card> cards = new HashMap<>();

    private static Map<Integer,Card> cacheCard = new HashMap<>(90);
    static {
        cards.put(CardType.PRIMARY_CARD.type,new PrimaryCard());
        cards.put(CardType.MEDIUM_CARD.type,new MediumCard());
        cards.put(CardType.ADVANCED_CARD.type,new AdvancedCard());
    }


    // 得分
    protected Integer score;

    // 卡牌类型
    protected Integer type;

    // 需要的宝石信息
    protected Map<Integer, Integer> gems;

    // 卡牌宝石buff
    protected Integer cardBuff;

    // 卡牌编号
    protected Integer no;

    private Integer residue;

    static {
        // 初始化宝石
        ArrayList<Card> c1 = new ArrayList<>(PrimaryCard.init());
        ArrayList<Card> c2 = new ArrayList<>(MediumCard.init());
        ArrayList<Card> c3 = new ArrayList<>(AdvancedCard.init());
        c1.addAll(c2);
        c1.addAll(c3);
        for (Card card : c1) {
            cacheCard.put(card.getNo(),card);
        }
    }

    public static Map<Integer,List<Card>> initCards(){
        Map<Integer,List<Card>> res = new HashMap();
        ArrayList<Card> c1 = new ArrayList<>(PrimaryCard.init());
        Collections.shuffle(c1);
        res.put(CardType.PRIMARY_CARD.getType(), c1);
        ArrayList<Card> c2 = new ArrayList<>(MediumCard.init());
        Collections.shuffle(c2);
        res.put(CardType.MEDIUM_CARD.getType(), c2);
        ArrayList<Card> c3 = new ArrayList<>(AdvancedCard.init());
        Collections.shuffle(c3);
        res.put(CardType.ADVANCED_CARD.getType(), c3);
        return res;
    }

    public static Map<Integer,Integer> findNByNoCard(Game game,CardInfo cardInfo){
        List<Card> cardList = game.getShowCards().get(cardInfo.getType());
        Integer no = cardInfo.getNo();
        int i = 0;
        for (; i < cardList.size(); i++) {
            if (no == cardList.get(i).getNo()){
                break;
            }
        }
        return cardList.get(i).getGems();
    }

    public static Map<Integer,Integer> getCardGems(Integer no){
        return cacheCard.get(no).getGems();
    }

    public static Card getCard(Integer no){
        return cacheCard.get(no);
    }
}
